﻿using System;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Web;

/// <summary>
/// Summary description for SendMail
/// </summary>
public class SendMail
{
    public SendMail()
    {
    }
    public string MailFrom { get; set; }
    public string MailTo { get; set; } //To separated by ; or ,
    public string MailBcc { get; set; } // BCC separated by ; or ,
    public string MailCC { get; set; } // CC separated by ; or ,
    public string MailSubject { get; set; }
    public string MailDisplayName { get; set; } // Display name of the sender
    public string MailBody { get; set; }
    public string MailPri { get; set; } // Mail priority 3-High, 2-Normal else Low
    public string MailNetClientUid { get; set; } //authentication details for the user
    public string MailNetClientPwd { get; set; }
    public string MailHost { get; set; } //smtp server name
    public string MailPort { get; set; } //smtp server port(if any)
    public bool SSlEnabled { get; set; } //Boolean value to indicate whether the connection with smtp server will be a secured one
    public string ReplyTo { get; set; }
    public string Attachment { get; set; }
    public string linkedresource_ID { get; set; }
    public string linkedresource_path { get; set; }
    public string alternateview_body { get; set; }
    public string Send()
    {
        string msg = string.Empty;
        MailMessage mailmsg = new MailMessage();
        try
        {
            mailmsg.From = new MailAddress(MailFrom, MailDisplayName, System.Text.Encoding.UTF8);
            //For multiple email addresses separated by , or ;
            MailAddressCollection list = new MailAddressCollection();
            if (MailTo.Contains(",") || MailTo.Contains(";"))
            {
                string[] allto = MailTo.Split(new char[] { ',', ';' });
                for (int i = 0; i < allto.Length; i++)
                {
                    list.Add(allto[i].ToString());
                    mailmsg.To.Add(list[i]);
                }

                list.Clear();
            }
            else
            {
                mailmsg.To.Add(MailTo);
            }
            //For BCC
            if (MailBcc != string.Empty && MailBcc != null)
            {
                if (MailBcc.Contains(",") || MailBcc.Contains(";"))
                {
                    string[] bccto = MailBcc.Split(new char[] { ',', ';' });
                    for (int i = 0; i < bccto.Length; i++)
                    {
                        list.Add(bccto[i].ToString());
                        mailmsg.Bcc.Add(list[i]);
                    }
                }
                else
                {
                    mailmsg.Bcc.Add(MailBcc);
                }
            }
            //For CC
            if (MailCC != string.Empty && MailCC != null)
            {
                if (MailCC.Contains(",") || MailCC.Contains(";"))
                {
                    string[] ccto = MailCC.Split(new char[] { ',', ';' });
                    for (int i = 0; i < ccto.Length; i++)
                    {
                        list.Add(ccto[i].ToString());
                        mailmsg.CC.Add(list[i]);
                    }
                }
                else
                {
                    mailmsg.CC.Add(MailCC);
                }
            }
            if (ReplyTo != null && ReplyTo != string.Empty)
            {
                mailmsg.Headers.Add("Reply-To", ReplyTo);
                // mailmsg.Headers.Add("Disposition-Notification-To", ReplyTo);
                //mailmsg.Headers.Add("Errors-To", ReplyTo);
                //mailmsg.Headers.Add("Disposition-OnFailure", ReplyTo);
                //mailmsg.Headers.Add("Disposition-OnFailure", ReplyTo);
                mailmsg.Headers.Add("Return-Path", ReplyTo);
            }
            //else
            //{
            //    mailmsg.Headers.Add("Reply-To", "kk@digitalsolutions.co.in");

            //}

            if (!string.IsNullOrEmpty(alternateview_body) && !string.IsNullOrEmpty(linkedresource_path))
            {
                LinkedResource logo = new LinkedResource(HttpContext.Current.Server.MapPath(linkedresource_path));
                logo.ContentId = linkedresource_ID.ToString();
                AlternateView alternate = AlternateView.CreateAlternateViewFromString(alternateview_body, null, "text/html");
                alternate.LinkedResources.Add(logo);
                mailmsg.AlternateViews.Add(alternate);
            }
            mailmsg.Subject = MailSubject;
            mailmsg.SubjectEncoding = System.Text.Encoding.UTF8;
            mailmsg.Body = MailBody;
            mailmsg.IsBodyHtml = true;
            mailmsg.BodyEncoding = System.Text.Encoding.UTF8;
            mailmsg.IsBodyHtml = true;
            mailmsg.Priority = (MailPri == "3") ? MailPriority.High : (MailPri == "2" ? MailPriority.Normal : MailPriority.Low);
            if (!string.IsNullOrEmpty(Attachment))
            {
                mailmsg.Attachments.Add(new Attachment(Attachment));
            }
            SmtpClient client = new SmtpClient();
            client.Credentials = new NetworkCredential(MailNetClientUid, MailNetClientPwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.Host = MailHost;
            client.EnableSsl = SSlEnabled;
            if (MailPort != "" && MailPort != null)
            {
                client.Port = Convert.ToInt32(MailPort);
            }
            //mailmsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
            mailmsg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess | DeliveryNotificationOptions.OnFailure | DeliveryNotificationOptions.Delay;
            //mailmsg.DeliveryNotificationOptions = DeliveryNotificationOptions.Delay;
            client.Send(mailmsg);
            msg = "Mail Sent Successfully...";
            mailmsg.Dispose();
        }
        catch (Exception ex)
        {
            Exception ex2 = ex;
            msg = string.Empty;
            while (ex2 != null)
            {
                msg += ex2.ToString();
                ex2 = ex2.InnerException;
            }
        }
        return msg;
    }
}
